package com.xwj.service.impl;

import com.xwj.dao.db1.OrderMapper;
import com.xwj.dao.db2.StorageMapper;
import com.xwj.domain.database1.Order;
import com.xwj.service.TestService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @author xwj
 * @date 2020/12/15
 */
@Service
public class TestServiceImpl implements TestService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private StorageMapper storageMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void test() {
        // 事务1：创建订单
        Order order = new Order();
        order.setUserId(13L);
        order.setProductId(1L);
        order.setCount(10);
        order.setMoney(BigDecimal.valueOf(100));
        order.setStatus(0);
        orderMapper.create(order);
        // 事务2：减少库存
        storageMapper.decrease(order.getProductId(), order.getCount());

        int i = 1 / 0;
    }
}
